/*--------------------------------------------------------------------------
    This source file is part of Abraxas Game Engine
    For the latest info, see http://code.google.com/abraxasge/
    Copyright (C) 2009, Andrew LeRoy <aleroy@member.igda.org>
    ---

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1301, USA.
--------------------------------------------------------------------------*/

#include "AbxPluginManager.hpp"
#include "AbxDynamicLibrary.hpp"
#include "AbxStdHeaders.hpp"


namespace Abx
{
    PluginManagerImpl::PluginManagerImpl()
    { }

    //-----------------------------------------------------------------------
    PluginManagerImpl::~PluginManagerImpl()
    {
        // Unload & delete resources in turn
        for (DynLibMap::iterator it = mLibMap.begin(); it != mLibMap.end(); ++it)
        {
            it->second->unload();
            //ABX_DELETE it->second;
            delete it->second;
        }

        // Empty the list
        mLibMap.clear();
    }
    
    //-----------------------------------------------------------------------
    DynamicLibrary* PluginManagerImpl::load(String const& filename)
    {
        DynLibMap::iterator i = mLibMap.find(filename);
        if (i != mLibMap.end())
        {
            return i->second;
        }
        else
        {
            //DynamicLibrary* pLib = ABX_NEW DynamicLibrary(filename);
            DynamicLibrary* pLib = new DynamicLibrary(filename);
            pLib->load();        
            mLibMap[filename] = pLib;
            return pLib;
        }
    }
    
    //-----------------------------------------------------------------------
    void PluginManagerImpl::unload(DynamicLibrary* lib)
    {
        DynLibMap::iterator i = mLibMap.find(lib->getName());
        if (i != mLibMap.end())
        {
            mLibMap.erase(i);
        }
        lib->unload();
        //ABX_DELETE lib;
        delete lib;
    }
    
} // namespace Abx
